package com.zboin.controller;

import com.zboin.common.domain.AjaxResult;
import com.zboin.common.domain.PageResult;
import com.zboin.domain.Article;
import com.zboin.domain.ArticleType;
import com.zboin.service.ArticleService;
import com.zboin.service.ArticleTypeService;
import com.zboin.service.FileManageService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author zhboom
 * @date 2025/5/9 22:47
 */
@RestController
@RequestMapping("/sys/article")
public class ArticleController {

    private final ArticleService articleService;
    private final FileManageService fileManageService;
    private final ArticleTypeService articleTypeService;

    public ArticleController(ArticleService articleService, FileManageService fileManageService, ArticleTypeService articleTypeService) {
        this.articleService = articleService;
        this.fileManageService = fileManageService;
        this.articleTypeService = articleTypeService;
    }

    @GetMapping("/{id}")
    public AjaxResult getArticleInfo(@PathVariable("id") Integer id) {
        return articleService.getArticleDetail(id);
    }

    @GetMapping("/content/{id}")
    public AjaxResult getArticleContent(@PathVariable("id") Integer id) {
        return articleService.getArticleContent(id);
    }

    @GetMapping("/list/{pageNum}/{pageSize}")
    public PageResult<Article> getFileList(Article article,
                                     @PathVariable("pageSize") Integer pageSize,
                                     @PathVariable("pageNum") Integer pageNum) {
        return articleService.list(pageNum, pageSize, article);
    }

    @GetMapping("/listByType/{typeId}")
    public AjaxResult getArticleListByType(@PathVariable("typeId") Integer typeId) {
        return articleService.getArticleListByType(typeId);
    }

    @PostMapping
    public AjaxResult addArticle(@RequestBody Article article) {
        return articleService.addArticle(article);
    }

    @PutMapping
    public AjaxResult updateArticle(@RequestBody Article article) {
        return articleService.updateArticle(article);
    }

    @DeleteMapping("/{id}")
    public AjaxResult deleteArticle(@PathVariable("id") Integer id) {
        return articleService.deleteArticle(id);
    }


    @PutMapping("/upload")
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) {
        return fileManageService.uploadFile(file, type);
    }

    @DeleteMapping("/delFile")
    public AjaxResult deleteFile(String filename) {
        return fileManageService.deleteFile(filename);
    }

    @GetMapping("/type/listAll")
    public AjaxResult getAllType() {
        return articleTypeService.getAllType();
    }

    @PostMapping("/type")
    public AjaxResult addArticleType(@RequestBody ArticleType articleType) {
        return articleTypeService.addArticleType(articleType);
    }
}
